//
//  SFNetCenter.swift
//  SwiftForwardWayXu
//
//  Created by 郑程旭 on 2022/1/11.
//

import Foundation
import UIKit
import Moya

enum SFNetApi {
    case apiSightPhoneLogin(params: [String: Any])
}

class SFNetCenter {
    
    static let center = SFNetCenter.init()
    private init() {}
    
    let requestProvider = MoyaProvider<SFNetApi>()
    
    private let userRegionKey = "userRegionKey"

    private var userRegion: Int {
        set {
            UserDefaults.standard.set(newValue, forKey: userRegionKey)
        }
        
        get {
            return UserDefaults.standard.object(forKey: userRegionKey) as! Int
        }
    }
    
    //MARK: - 设置用户区域
    public func configUserRegion(userRegion: Int) {
        self.userRegion = userRegion
    }
        
    //MARK: - 0.广西 1.全国 2.15省 3.廊坊 4.普瑞
    public func getLoginBaseUrl() -> String {
        
        var loginBaseUrl: String?
        
        switch self.userRegion {
            
        case 0:
            loginBaseUrl = "https://api.eyenurse.net/"
            break
            
        case 1:
            loginBaseUrl = "https://www.guozikeji.com/"
            break
            
        case 2:
            loginBaseUrl = "https://www.guozikeji.com/"
            break
            
        case 3:
            loginBaseUrl = "https://xsjk.wjwxxzx.cn/"
            break
            
        case 4:
            loginBaseUrl = "http://116.153.70.68:9001/"
            break
            
        default:
            break
        }
        
        return loginBaseUrl!
    }
    
    static public func netCenterPostRequest(api: SFNetApi, success: @escaping (([String: Any]) -> ()), failure: @escaping (() -> ()), complete: @escaping (() -> ())) {
        center.requestProvider.request(api) { result in
            switch result {
                case .success(let response):
                let responseDic = try? response.mapJSON()
                if responseDic == nil {
                    failure()
                    complete()
                } else {
                    let successDic = responseDic as! [String: Any]
                    let error = successDic["error"]! as! String
                    guard error == "-1" else {
                        failure()
                        complete()
                        return
                    }
                    success(successDic)
                    complete()
                }
    
                break
            case .failure(_):
                failure()
                complete()
                break
            }
        }
    }
    
}


extension SFNetApi: TargetType {
    var baseURL: URL {
        switch self {
        case .apiSightPhoneLogin(let params):
            let account = params["phone"] as! String
            let cdtStr = account[..<account.index(account.startIndex, offsetBy: 2)]
            if cdtStr == "a6" {
                SFNetCenter.center.configUserRegion(userRegion: 4)
            }
            return URL(string: SFNetCenter.center.getLoginBaseUrl())!
        }
    }
    
    var path: String {
        switch self {
        case .apiSightPhoneLogin:
            return "newdeparture/sight/phoneLogin"
        }
    }
    
    var method: Moya.Method {
        return .post
    }
    
    var task: Task {
        var tempParams: [String: Any] = [:]
        
        switch self {
        case .apiSightPhoneLogin(let params):
            let jsonData:Data! = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
            return .requestCompositeData(bodyData: jsonData, urlParameters: params)
        }
        
    }
    
    var headers: [String : String]? {
        return ["Content-type": "application/json"]
    }
    
    
}















